package top.zlhy7.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author renyong
 * @date 2024/9/30 01:39
 * @description 时间工具类
 */
public class DateUtils{
    /**
     * 获取范围日期的指定连续时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param amountToAdd 时间拨动数量级
     * @param unit 时间单元
     * @return
     * @author renyong on 2021/7/6 12:08
     */
    public static List<LocalDateTime> getBetweenDate(LocalDateTime startTime, LocalDateTime endTime, long amountToAdd, TemporalUnit unit){
        return getBetweenDate(startTime,endTime,amountToAdd,unit, Function.identity());
    }
    /**
     * 获取范围日期的指定连续时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param amountToAdd 时间拨动数量级
     * @param unit 时间单元
     * @return
     * @author renyong on 2021/7/6 12:08
     */
    public static<T> List<T> getBetweenDate(LocalDateTime startTime, LocalDateTime endTime, long amountToAdd,
                                            TemporalUnit unit, Function<LocalDateTime,T> function){
        List<T> tList = new ArrayList<>();
        for (LocalDateTime startTimeTemp = startTime; startTimeTemp.isBefore(endTime) || startTimeTemp.isEqual(endTime);startTimeTemp = startTimeTemp.plus(amountToAdd, unit)) {
            tList.add(function.apply(startTimeTemp));
        }
        return tList;
    }
    /**
     * 获取某月连续日期
     * @param localDate 某月的一天
     * @return
     * @author renyong on 2024/9/30 01:40
     */
    public static List<LocalDate> getBetweenDate(LocalDate localDate){
        int maxDay = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        List<LocalDate> betweenDate = new ArrayList<>(maxDay);
        LocalDate startTime = localDate.with(TemporalAdjusters.firstDayOfMonth());
        for (int i = 0; i < maxDay; i++) {
            betweenDate.add(startTime.plusDays(i));
        }
        return betweenDate;
    }
}
